package com.edu.gcu.manager.controller;

import com.edu.gcu.manager.service.ISystemService;
import com.edu.gcu.system.common.Const;
import com.edu.gcu.system.common.ResponseCode;
import com.edu.gcu.system.common.ServerResponse;
import com.edu.gcu.system.pojo.Admin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @author polo
 * @version V1.0
 * @description: 后台系统控制器
 * @date 2018-02-20-12:09
 */

@Controller
@RequestMapping("/manage/system")
public class SystemController {

    private static Logger logger = LoggerFactory.getLogger(SystemController.class);

    @Autowired
    private ISystemService iSystemService;


    /**
     * @description 用户登录
     * @author polo
     */
    @ResponseBody
    @RequestMapping("login.do")
    public ServerResponse login(String username, String password, HttpSession session) {
        ServerResponse response = iSystemService.login(username, password);
        if (response.isSuccess()) {
            session.setAttribute(Const.CURRENT_ADMIN, response.getData());
        }
        return response;
    }

    /**
     * @description 用户退出
     * @author polo
     */
    @RequestMapping("logout.do")
    public String logout(HttpSession session) {

        session.removeAttribute(Const.CURRENT_ADMIN);
        return "manage/login";
    }

    /**
     * @description 用户个人详情
     * @author polo
     */
    @ResponseBody
    @RequestMapping("getInfo.do")
    public ServerResponse getInfo(HttpSession session) {
        Admin currentAdmin = (Admin) session.getAttribute(Const.CURRENT_ADMIN);
        if (currentAdmin == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN
                    .getDesc());
        }
        return ServerResponse.createBySuccess(session.getAttribute(Const.CURRENT_ADMIN));
    }

    /**
     * @description 用户忘记密码
     * @author polo
     */
    @ResponseBody
    @RequestMapping("forgetCheckUser.do")
    public ServerResponse forgetCheckUser(String username, String name, HttpSession session) {
        return iSystemService.forgetCheckUser(username, name);
    }

    /**
     * @description 用户忘记密码重置密码
     * @author polo
     */
    @ResponseBody
    @RequestMapping("forgetUpdatePwd.do")
    public ServerResponse forgetUpdatePwd(String token, String username, String password, HttpSession session) {
        return iSystemService.forgetUpdatePwd(token, username, password);
    }

    /**
     * @description 登录状态修改个人信息
     * @author polo
     */
    @ResponseBody
    @RequestMapping("updateInfo.do")
    public ServerResponse updateInfo(Admin admin, HttpSession session) {
        Admin currentAdmin = (Admin) session.getAttribute(Const.CURRENT_ADMIN);
        if (currentAdmin == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN
                    .getDesc());
        }
        admin.setId(currentAdmin.getId());
        admin.setUsername(currentAdmin.getUsername());
        ServerResponse response = iSystemService.updateById(admin);
        if (response.isSuccess()) {
            session.setAttribute(Const.CURRENT_ADMIN, response.getData());
        }
        return response;
    }
}
